package com.zrrd.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrrd.common.page.ImageClassPageVo;
import com.zrrd.common.vo.ImageClassVo;
import com.zrrd.entity.Image;
import com.zrrd.entity.ImageClass;
import com.zrrd.mapper.ImageClassMapper;
import com.zrrd.service.ImageClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.service.ImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanjl
 * @since 2024-09-12
 */
@Service
public class ImageClassServiceImpl extends ServiceImpl<ImageClassMapper, ImageClass> implements ImageClassService {

    @Resource
    private ImageService imageService;
    /**
     * 分页查询相册
     *
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    @Override
    public ImageClassPageVo findByPage(Integer page, Integer limit, String keyword) {
        ImageClassPageVo vo = new ImageClassPageVo();
        PageHelper.startPage(page, limit);
        List<ImageClass> list = lambdaQuery().orderByAsc(ImageClass::getOrdered).list();
        PageInfo<ImageClass> pageInfo = new PageInfo<>(list);
        List<ImageClassVo> imageClassVoList = pageInfo.getList().stream().map(imageClass -> {
            ImageClassVo imageClassVo = new ImageClassVo();
            BeanUtils.copyProperties(imageClass, imageClassVo);
            Integer count = imageService.lambdaQuery().eq(Image::getImageClassId, imageClass.getId()).count();
            imageClassVo.setImagesCount(count);
            return imageClassVo;
        }).collect(Collectors.toList());
        vo.setImages(imageClassVoList);
        vo.setTotalCount(pageInfo.getTotal());
        return vo;
    }
}
